package gam.n1.areas.a;

import gam.n1.achievement.n.LongAchievement;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

/**
 * 计时
 */
public abstract class TimingBaseBukkitArea extends BaseBukkitAreaCustom {
    protected long t;
    protected volatile long pT;

    public TimingBaseBukkitArea(@NotNull Plugin plugin, @NotNull String key) {
        super(plugin, key);
        this.t = 0;
        this.pT = 0;
        getAchievementManager().addRule(new LongAchievement(()->pT,"PassTime"));
    }
    public void setTime(long time){
        if (t == 0 && !isRunning()){
            t = time;
        }
    }

    @Override
    public void afterAction() {
        super.afterAction();
        this.pT = 0;
    }

    @Override
    public boolean cI() {
        return super.cI() && t != 0;
    }

    @Override
    public boolean cD() {
        return pT == -1;
    }
    @Override
    public boolean beforeEnd() {
        return super.beforeEnd() || (this.pT = System.currentTimeMillis() - this.currentTime) >= t;
    }
    protected void toEnd(){
        this.pT = t + 1;
    }

    @Override
    protected void rS() {
        super.rS();
        this.pT = -1;
    }
}
